﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;

using CP.Utility;
using CP.Controllers.GPM;
using CP.Entities;

namespace GPM
{
    public partial class FrmSetUserRole : BaseCommForm
    {
        public GridCheckMarksSelection selection;
        private User_Info User;
        public FrmSetUserRole()
        {
            InitializeComponent();
            this.BarComponents = components;
        }
        public FrmSetUserRole(User_Info user)
            : this()
        {
            this.User = user;
            this.txtUserName.Text = user.user_name;
            this.lookupGroupID.EditValue = user.group_id;
        }

        private void FrmSetUserRole_Load(object sender, EventArgs e)
        {
            selection = new GridCheckMarksSelection(this.gvUserGroup);
            selection.SelectionChanged += new DevExpress.Data.SelectionChangedEventHandler(selection_SelectionChanged);
            UserGroupController uc = new UserGroupController();
            IList<User_Group> groups = uc.GetAllUserGroups();
            this.lookupGroupID.Properties.DataSource = groups;
            this.lookupeditParentID.DataSource = groups;
            this.gcUserGroup.DataSource = groups;

            SetGroupChecked(groups, User);
        }

        void selection_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            this.txtSelectedRoleNum.Text = selection.SelectedCount.ToString() + "个角色已选中";
            lbSelectedRols.Items.Clear();
            foreach (object o in selection.Selection)
            {
                User_Group group = o as User_Group;
                if (null != group)
                    lbSelectedRols.Items.Add(group.group_name);
            }
        }

        private void SetGroupChecked(IList<User_Group> groups, User_Info user)
        {
            UserGroupController uc = new UserGroupController();
            IList<User_Group_Relation> rels = uc.GetUserRelations(user.user_id);
            IList<int> groupAuthed = new List<int>();
            rels.ForEach(r => groupAuthed.Add(r.group_id));

            for (int i = 0; i < groups.Count; i++)
            {
                if (groupAuthed.Contains(groups[i].group_id))
                    selection.SelectRow(i, true);
            }
        }

        private bool CheckInput()
        {
            if (string.IsNullOrEmpty(txtUserName.Text))
            {
                ExMessageBox.Show("提示信息：用户名不能为空！", AppSetter.SoftName);
                return false;
            }

            if (this.lookupGroupID.EditValue == null)
            {
                ExMessageBox.Show("提示信息：默认角色不能为空！", AppSetter.SoftName);
                return false;
            }

            return true;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!CheckInput())
                return;
            IList<int> groupids = new List<int>();
            foreach (object o in selection.Selection)
            {
                User_Group group = o as User_Group;
                if (null != group)
                    groupids.Add(group.group_id);
            }
            try
            {
                UserGroupController gc = new UserGroupController();
                gc.SetUserRole(User.user_id, Convert.ToInt32(lookupGroupID.EditValue), groupids);

                ExMessageBox.Show("提示信息：保存成功！", AppSetter.SoftName);
                LogHelper.Log("设置角色", "update", "userid:" + User.user_id, "default role:" + lookupGroupID.EditValue.ToString(), "groupids:" + string.Join(",", groupids));
                UserInfoController uc = new UserInfoController(this.Owner.ActiveMdiChild as FrmUserInfo);
                uc.QueryUserInfo();
            }
            catch (Exception ex)
            {
                ExMessageBox.Show(ex.Message, AppSetter.SoftName);
            }
        }

    }
}
